using Microsoft.DirectX.Direct3D;
using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace LANLauncher
{
	public class DXFuncs
	{
		private const float epsilon = 0.01f;

		public DXFuncs()
		{
		}

		public bool AddAdapters(Settings _settings)
		{
			Setting setting = _settings.GetSetting("Adapter");
			bool flag = false;
			for (int i = 0; i < Manager.Adapters.Count; i++)
			{
				AdapterInformation item = Manager.Adapters[i];
				AdapterDetails information = item.Information;
				if (setting.AddValue(string.Concat(information.Description, information.DeviceName)))
				{
					this.RefreshResolutionList(_settings, item);
					flag = true;
				}
			}
			return flag;
		}

		public void GetAnisotropicValues(Settings _settings)
		{
			Setting setting = _settings.GetSetting("Anisotropic Filtering");
			setting.Clear();
			int maxAnisotropy = Manager.GetDeviceCaps(0, DeviceType.Hardware).MaxAnisotropy;
			int[] numArray = new int[] { 4, 8, 16, 16 };
			string[] strArrays = new string[] { "1", "2", "3", "4" };
			bool flag = false;
			int num = 0;
			for (int i = 0; i < (int)numArray.Length; i++)
			{
				if (maxAnisotropy >= numArray[i])
				{
					flag = flag | setting.AddValue(strArrays[i]);
					num = i;
				}
			}
			if (num > 2)
			{
				if (!flag)
				{
					setting.SetSelectedItem("2");
				}
				setting.SetDefaultValue("2");
				return;
			}
			if (!flag)
			{
				setting.SetSelectedItem(num.ToString());
			}
			setting.SetDefaultValue(num.ToString());
		}

		public int GetNumAdapters()
		{
			return Manager.Adapters.Count;
		}

		private bool IsSupportedResolution(float _width, float _height)
		{
			float single = _width / _height;
			if (Math.Abs(1.5f - single) >= 0.01f && Math.Abs(1.33333337f - single) >= 0.01f && Math.Abs(1.25f - single) >= 0.01f && Math.Abs(1.66666663f - single) >= 0.01f && Math.Abs(1.77777779f - single) >= 0.01f && Math.Abs(1.6f - single) >= 0.01f && Math.Abs(1.88888884f - single) >= 0.01f)
			{
				return false;
			}
			return true;
		}

		public void RefreshResolutionList(Settings _settings, string _adapterString)
		{
			for (int i = 0; i < Manager.Adapters.Count; i++)
			{
				AdapterInformation item = Manager.Adapters[i];
				AdapterDetails information = item.Information;
				if (string.Concat(information.Description, information.DeviceName) == _adapterString)
				{
					this.RefreshResolutionList(_settings, item);
					return;
				}
			}
		}

		public void RefreshResolutionList(Settings _settings, int _adapterIndex)
		{
			this.RefreshResolutionList(_settings, Manager.Adapters[_adapterIndex]);
		}

		private void RefreshResolutionList(Settings _settings, AdapterInformation _info)
		{
			Setting setting = _settings.GetSetting("Resolution");
			setting.Clear();
			bool flag = false;
			foreach (DisplayMode supportedDisplayMode in _info.SupportedDisplayModes)
			{
				if (supportedDisplayMode.Format != Format.X8R8G8B8 || supportedDisplayMode.Width < 800 || supportedDisplayMode.Height < 600 || !this.IsSupportedResolution((float)supportedDisplayMode.Width, (float)supportedDisplayMode.Height))
				{
					continue;
				}
				object[] width = new object[] { supportedDisplayMode.Width, "x", supportedDisplayMode.Height, "x", supportedDisplayMode.RefreshRate };
				flag = flag | setting.AddValue(string.Concat(width));
			}
			ComboBox attachedControl = null;
			try
			{
				attachedControl = (ComboBox)setting.AttachedControl;
			}
			catch
			{
				return;
			}
			int num = attachedControl.Items.IndexOf("1280x720x60");
			if (num < 0)
			{
				num = attachedControl.Items.IndexOf("800x600x60");
			}
			if (num < 0)
			{
				num = 0;
			}
			if (!flag)
			{
				attachedControl.SelectedIndex = num;
				string str = attachedControl.Items[num].ToString();
				string str1 = str;
				setting.DefaultValue = str;
				setting.WorkingValue = str1;
				return;
			}
			setting.WorkingValue = attachedControl.SelectedItem.ToString();
			setting.DefaultValue = attachedControl.Items[num].ToString();
		}

		public bool SupportsDirectX11()
		{
			if (Environment.OSVersion.Version.Major < 6)
			{
				return false;
			}
			if (Environment.OSVersion.Version.Major > 6 || Environment.OSVersion.Version.Minor >= 1)
			{
				return true;
			}
			if (File.Exists(string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.System), "\\d3d11.dll")))
			{
				return true;
			}
			return false;
		}
	}
}